from base import Yaml, Path, build_request, Json
from page.nsp_user_api import NspUserApi


class NspCartGetCurrentApi(NspUserApi):
    """
    用途：在接口端前台买家登录查看购物车数据方法
    作者：曹峰
    时间：2019-5-6
    邮箱：243934857@qq.com
    """

    file = Path.get_actual_path_by_current_file(__file__, 'nsp.yaml')
    _cart_yanml = Yaml.get_config_as_dict(file)['NspCartGetCurrentApi']

    def get_cart_goods(self, sign=None, token=None):
        """
        前台买家查询购物车
        :param sign: 签名
        :param token: 秘钥
        :return:
                ret
                data
                   result
                       cart_id
                       buyer_id
                       shop_id
                       shop_name
                       goods_id
                       goods_name
                       sku_id
                       sku_name
                       price
                       num
                       goods_picture
                       bl_id
                   code
                   msg
                msg
        """
        sign, token = self._get_to_web_sign_token(sign, token)

        url = '%s?s=%s' % (self._base_url, self._cart_yanml['CART_URL'])
        data = {'sign': sign}
        req = build_request(url=url, method='p', auth=token, params=data)

        return self._get_cart_detail_resp(req.json_dict)

    def _get_cart_detail_resp(self, json_dict):
        """
        解析json_dict
        :param json_dict:
        :return:
                result
                       cart_id
                       buyer_id
                       shop_id
                       shop_name
                       goods_id
                       goods_name
                       sku_id
                       sku_name
                       price
                       num
                       goods_picture
                       bl_id
                   code
                   msg
        """
        cart_detail_resp = self._get_parse_http_resp(json_dict)

        cart_detail_resp['cart_id'] = Json.parse_json_dict_value(
            json_dict,
            data_key=self._cart_yanml[
                'CART_ID_DATAKET'],
            index=self._cart_yanml[
                'CART_ID_INDEX'],
            sub_key=self._cart_yanml[
                'CART_ID']
        )

        cart_detail_resp['buyer_id'] = Json.parse_json_dict_value(
            json_dict,
            data_key=self._cart_yanml[
                'CART_BUYER_ID_DATAKET'],
            index=self._cart_yanml[
                'CART_BUYER_ID_INDEX'],
            sub_key=self._cart_yanml[
                'CART_BUYER_ID']
        )

        cart_detail_resp['shop_id'] = Json.parse_json_dict_value(
            json_dict,
            data_key=self._cart_yanml[
                'CART_SHOP_ID_DATAKEY'],
            index=self._cart_yanml[
                'CART_SHOP_ID_INDEX'],
            sub_key=self._cart_yanml[
                'CART_SHOP_ID']
        )

        cart_detail_resp['shop_name'] = Json.parse_json_dict_value(
            json_dict,
            data_key=self._cart_yanml[
                'CART_SHOP_NAME_DATAKEY'],
            index=self._cart_yanml[
                'CART_SHOP_NAME_INDEX'],
            sub_key=self._cart_yanml[
                'CART_SHOP_NAME']
        )
        cart_detail_resp['goods_id'] = Json.parse_json_dict_value(
            json_dict,
            data_key=self._cart_yanml[
                'CART_GOODS_ID_DATAKEY'],
            index=self._cart_yanml[
                'CART_GOODS_ID_INDEX'],
            sub_key=self._cart_yanml[
                'CART_GOODS_ID']
        )

        cart_detail_resp['goods_name'] = Json.parse_json_dict_value(
            json_dict,
            data_key=self._cart_yanml[
                'CART_GOODS_NAME_DATAKEY'],
            index=self._cart_yanml[
                'CART_GOODS_NAME_INDEX'],
            sub_key=self._cart_yanml[
                'CART_GOODS_NAME']
        )

        cart_detail_resp['sku_id'] = Json.parse_json_dict_value(
            json_dict,
            data_key=self._cart_yanml[
                'CART_SKU_ID_DATAKEY'],
            index=self._cart_yanml[
                'CART_SKU_ID_INDEX'],
            sub_key=self._cart_yanml[
                'CART_SKU_ID']
        )

        cart_detail_resp['sku_name'] = Json.parse_json_dict_value(
            json_dict,
            data_key=self._cart_yanml[
                'CART_SKU_NAME_DATAKEY'],
            index=self._cart_yanml[
                'CART_SKU_NAME_INDEX'],
            sub_key=self._cart_yanml[
                'CART_SKU_NAME']
        )

        cart_detail_resp['price'] = Json.parse_json_dict_value(
            json_dict,
            data_key=self._cart_yanml[
                'CART_PRICE_DATAKEY'],
            index=self._cart_yanml[
                'CART_PRICE_INDEX'],
            sub_key=self._cart_yanml[
                'CART_PRICE']
        )

        cart_detail_resp['num'] = Json.parse_json_dict_value(
            json_dict,
            data_key=self._cart_yanml[
                'CART_NUM_DATAKEY'],
            index=self._cart_yanml[
                'CART_NUM_INDEX'],
            sub_key=self._cart_yanml[
                'CART_NUM']
        )

        cart_detail_resp['goods_picture'] = Json.parse_json_dict_value(
            json_dict,
            data_key=self._cart_yanml[
                'CART_GOODS_PICTURE_DATAKET'],
            index=self._cart_yanml[
                'CART_GOODS_PICTURE_INDEX'],
            sub_key=self._cart_yanml[
                'CART_GOODS_PICTURE']
        )

        cart_detail_resp['bl_id'] = Json.parse_json_dict_value(
            json_dict,
            data_key=self._cart_yanml[
                'CART_BL_ID_DATAKEY'],
            index=self._cart_yanml[
                'CART_BL_ID_INDEX'],
            sub_key=self._cart_yanml[
                'CART_BL_ID']
        )

        cart_detail_resp['json_dict'] = json_dict

        return cart_detail_resp
